package m2dl.ter.mirrorgame;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.TextView;

public class Choregraphy extends Activity {
	

	final String EXTRA_GESTURES = "gestures";
	
	private TextView gestureEvent;
	private final static int SWIPE_UP = 0;
	private final static int SWIPE_DOWN = 1;
	private final static int SWIPE_LEFT = 2;
	private final static int SWIPE_RIGHT = 3;
	protected ArrayList<Integer> gestureList = new ArrayList<Integer>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_choregraphy);
		gestureEvent = (TextView) findViewById(R.id.gestureEvent);
		gestureEvent.setText("Movements :");
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return gestureDetector.onTouchEvent(event);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.choregraphy, menu);
		return true;
	}

	@Override
	public void onStop() {
		super.onStop();
		System.exit(RESULT_OK);
		this.finish();
	}

	// Manages the menu
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.choregraphy_quitter:
			finish();
			return true;
		}
		return false;
	}

	private SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener() {

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {

			int swipe = 0;
			float deltaLeft = (e1.getX() - e2.getX());
			float deltaRight = (e2.getX() - e1.getX());
			float deltaUp = (e1.getY() - e2.getY());
			float deltaDown = (e2.getY() - e1.getY());
			float sensitvity = 50;

			if (deltaLeft > sensitvity) {
				swipe = SWIPE_LEFT;
			} else if (deltaRight > sensitvity) {
				swipe = SWIPE_RIGHT;
			}

			if (deltaUp > sensitvity) {
				switch (swipe) {
				case SWIPE_LEFT:
					if (deltaUp > deltaLeft) {
						swipe = SWIPE_UP;
					}
					break;
				case SWIPE_RIGHT:
					if (deltaUp > deltaRight) {
						swipe = SWIPE_UP;
					}
					break;
				default:
					swipe = SWIPE_UP;
					break;
				}

			} else if (deltaDown > sensitvity) {
				switch (swipe) {
				case SWIPE_LEFT:
					if (deltaDown > deltaLeft) {
						swipe = SWIPE_DOWN;
					}
					break;
				case SWIPE_RIGHT:
					if (deltaDown > deltaRight) {
						swipe = SWIPE_DOWN;
					}
					break;
				default:
					swipe = SWIPE_DOWN;
					break;
				}
			} else {

			}
			addGesture(swipe);
			return super.onFling(e1, e2, velocityX, velocityY);
		}
	};

	GestureDetector gestureDetector = new GestureDetector(
			simpleOnGestureListener);

	protected void addGesture(int swipe_direction) {

		if (this.gestureList.size() < 10) {
			this.gestureList.add(swipe_direction);
			ImageView icon = (ImageView) findViewById(R.id.swipeIcon);
			switch (swipe_direction) {
			case SWIPE_UP:
				icon.setBackgroundResource(R.drawable.arrow_up);
				gestureEvent.setText(gestureEvent.getText()+" Up");
				break;
			case SWIPE_DOWN:
				icon.setBackgroundResource(R.drawable.arrow_down);
				gestureEvent.setText(gestureEvent.getText()+" Down");
				break;
			case SWIPE_RIGHT:
				icon.setBackgroundResource(R.drawable.arrow_right);
				gestureEvent.setText(gestureEvent.getText()+" Right");
				break;
			case SWIPE_LEFT:
				icon.setBackgroundResource(R.drawable.arrow_left);
				gestureEvent.setText(gestureEvent.getText()+ " Left");
				break;
			default:
				break;
			}
			// TableRow tableau = (TableRow) findViewById(R.id.tableau);
			// tableau.addView(icon);
			// tableau.getLayoutParams();
		} else {
			Intent intent = new Intent(Choregraphy.this, ChangePlayer.class);
			intent.putExtra(EXTRA_GESTURES, this.gestureList);
			startActivity(intent);
			System.exit(RESULT_OK);
			// TODO Passer le ArrayList
		}
	}
}
